<?php
function bb2html($string)
{
global $imgtag;
    $string = str_replace( "\r\n",  "<br>", $string);
    $string = str_replace( "\r",  "\n", $string);

    $string = preg_replace("/\[ut\](.*?)\[\/ut\]/si", "<marquee direction=up height=160>\\1</marquee>", $string);
    $string = preg_replace("/\[dt\](.*?)\[\/dt\]/si", "<marquee direction=down height=160>\\1</marquee>", $string);
    $string = preg_replace("/\[lt\](.*?)\[\/lt\]/si", "<marquee direction=left>\\1</marquee>", $string);
    $string = preg_replace("/\[rt\](.*?)\[\/rt\]/si", "<marquee direction=right>\\1</marquee>", $string);
    $string = preg_replace("/\[br\](.*?)/si","<br>", $string);
    $string = preg_replace("/\[b\](.*?)\[\/b\]/si", "<b>\\1</b>", $string);
    $string = preg_replace("/\[i\](.*?)\[\/i\]/si", "<i>\\1</i>", $string);
    $string = preg_replace("/\[u\](.*?)\[\/u\]/si", "<u>\\1</u>", $string);
    $string = preg_replace("/\[p\](.*?)\[\/p\]/si", "<p>\\1</p>", $string);
    $string = preg_replace("/\[s\](.*?)\[\/s\]/si", "<s>\\1</s>", $string);
    $string = preg_replace("/\[m\](.*?)\[\/m\]/si", "<tt>\\1</tt>", $string);
    $string = preg_replace("/\[sup\](.*?)\[\/sup\]/si", "<sup>\\1</sup>", $string);
    $string = preg_replace("/\[sub\](.*?)\[\/sub\]/si", "<sub>\\1</sub>", $string);
    $string = preg_replace("/\[list\](.*?)\[\/list\]/si", "<ul>\\1</ul>", $string);
    $string = preg_replace("/\[spoiler\](.*?)\[\/spoiler\]/si", "<span style=\"COLOR: black; TEXT-DECORATION: none; background-color: black; font-weight: normal;\" onmouseover=\"this.style.color='#FFFFFF';\" onmouseout=\"this.style.color=this.style.backgroundColor='#000000'\">\\1</span>", $string);
    $string = preg_replace("/\[font color=(\S+?)\](.*?)\[\/font\]/si",
                           "<font color=\"\\1\">\\2</font>", $string);
    $string = preg_replace("/\[color=(\S+?)\](.*?)\[\/color\]/si",
                           "<font color=\"\\1\">\\2</font>", $string);
    $string = preg_replace("/\[font size=([1-8])\](.*?)\[\/font([1-8])\]/si",
                           "<font size=\"\\1\">\\2</font>", $string);
    $string = preg_replace("/\[s([1-8])\](.*?)\[\/s([1-8])\]/si",
                           "<font size=\"\\1\">\\2</font>", $string);
    $string = preg_replace("/\[blink\](.*?)\[\/blink\]/si", "<blink>\\1</blink>", $string);
    $string = preg_replace("/\[aa\](.*?)\[\/aa\]/si", '<div class="aa">\\1</div>', $string);
    $string = preg_replace("/(^|[^=\]h])(ttp:)/si", "\\1http:", $string);
    $length = 60;
    preg_match_all("/(^|[^=\]])(https?:\/\/[\!-;\=\?-\~]+)/si", $string,$reg);
    for($i=0;$i<count($reg[0]);$i++){
      $out[$i] = (strlen($reg[2][$i]) > $length) ? substr($reg[2][$i],0,$length)."..." : $reg[2][$i];
      $string = str_replace($reg[0][$i],$reg[1][$i]."<a href=".$reg[2][$i]." target=_blank>".$out[$i]."</a>",$string);
    }
    $string = preg_replace("/\[url\](http|https|ftp)(:\/\/\S+?)\[\/url\]/si",
                           "<a href=\"\\1\\2\" target=\"_blank\">\\1\\2</A>", $string);
    $string = preg_replace("/\[url\](\S+?)\[\/url\]/si",
                           "<a href=\"http://\\1\" target=\"_blank\">\\1</A>", $string);
    $string = preg_replace("/\[url=(http|https|ftp)(:\/\/\S+?)\](.*?)\[\/url\]/si",
                           "<a href=\"\\1\\2\" target=\"_blank\">\\3</A>", $string);
    $string = preg_replace("/\[url=(\S+?)\](\S+?)\[\/url\]/si",
                           "<a href=\"http://\\1\" target=\"_blank\">\\2</A>", $string);
    $string = preg_replace("/\[email\](\S+?@\S+?\\.\S+?)\[\/email\]/si",
                           "<a href=\"mailto:\\1\">\\1</A>", $string);
    $string = preg_replace("/\[email=(\S+?@\S+?\\.\S+?)\](.*?)\[\/email\]/si",
                           "<a href=\"mailto:\\1\">\\2</A>", $string);
    $string = preg_replace("#\[img\](([a-z]+?)://([^ \n\r]+?))\[\/img\]#si",
                           "<img src=\"\\1\" border=0 alt=\"\\1\">", $string);
    $string = preg_replace("#\[bigimg\](([a-z]+?)://([^ \n\r]+?))\[\/bigimg\]#si",
                           "<a href=\"\\1\" target=\"_blank\"><img class=\"postimg\" src=\"\\1\" border=0 alt=\"\\1\"></a>", $string);
    return $string;
}
?>